import { queryUserRing, bindUserRing } from "@/api/ring/bind.js";
import store from "@/store/index";

export const useQueryUserRing = () => {
	const AccessToken = uni.getStorageSync("userToken");
	if (!AccessToken) {
		uni.redirectTo({ url: "/pages/user/login" });
		return;
	}
};

export const useBindUserRing = async (data) => {
	const bindRingError = {
		email: "",
		flag: false,
		reason: "",
		status: 0,
		check: false,
	};
	try {
		const result = await bindUserRing(data);
		const type = Number(result.code);
		console.log("戒指绑定状态码", res);

		switch (type) {
			case 200:
				bindRingError.email = "";
				bindRingError.flag = false;
				bindRingError.reason = "";
				bindRingError.status = type;

				// setTimeout(() => {
				// 	uni.redirectTo({ url: "/pages/index/index" });
				// }, 500);

				break;

			default:
				const { msg, data } = result;
				bindRingError.email = "";
				bindRingError.flag = true;
				bindRingError.reason = msg;
				bindRingError.status = type;
				break;
		}
	} catch (error) {}
};

export const getUserRing = async () => {
	try {
		const { data } = await queryUserRing();
		if (!data) return Promise.resolve("");
		console.log('💍data💍', data);
		
		store.dispatch("ringDataManage/setRing", {
			name: data.name,
			mac: data.mac,
			customName: data.customName,
			ringType: data.ringType
		});

		return Promise.resolve(data);
	} catch (error) {
		console.log('获取戒指失败', error);
		if (error.code === 800) {
			return Promise.resolve(800);
		}
		return Promise.resolve("");
	}
};
